// - 游戏场景
// -Game.js
// -author: lbfamous
// -date: 2020-02-09

var GameData = require('GameData');;

cc.Class({
    extends: cc.Component,

    properties: {

        prefab:{
            default:null,
            type:cc.Prefab,
        },

        fishpool: {
            default:null,
            type:cc.Node,
        },
        hook:{
            default:null,
            type:cc.Node,
        },
        depth:{
            default:0,
            type:cc.Float,
            visible:false,
        },
        sceneData:{
            default:null,
            type: cc.JsonAsset,
        },

        index:{
            default: 0,
            visible:false,
        },

        controller:{
            default:null,
            type:cc.Node,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        manager.enabledDebugDraw = true;
        
    },

    start () {

    },

    update (dt) {
        
        // let depth = Math.floor(Math.abs(this.hook.y) /100);
        let data = this.sceneData.json[this.index];        

        this.depth = Math.floor(Math.abs(this.hook.y) / 100);
        if(data != undefined && this.depth >= data.depth){

            // console.log('data.res ' + data.res);
            let fish = cc.instantiate(this.prefab)
            cc.loader.loadRes(data.res,cc.SpriteFrame,function(err,spriteFrame){
                // console.log("err " + err);
                if(!err){
                    
                    fish.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                }
            });

            fish.x = Math.random() * 640 - 320;
            fish.y = this.hook.y - 480 - 100;
            this.fishpool.addChild(fish)
            this.index++;

        }

        console.log('depth = ',this.depth)
        GameData.instance.depth = Math.max(GameData.instance.depth , this.depth)
    },

    reset(){
        GameData.instance.depth = 0;
        GameData.instance.score = 0;
        
        this.fishpool.removeAllChildren(true);
        this.controller.removeAllChildren(true);
        this.controller.x = 0;
    }
});
